package dn.easy.common.commonwidget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wang.avi.AVLoadingIndicatorView;

import dn.easy.common.R;
import dn.easy.common.commonutils.StringUtils;


/**
 * description:界面过度加载动画
 * Created by 丁宁
 * on 2017.05
 */
public class AVLoadingDialog {
    private static AVLoadingIndicatorView avi;
    /**
     * 加载数据对话框
     */
    private static Dialog mLoadingDialog;
    private static TextView portF;

    public static Dialog showDialogForLoading(Context context, String msg) {
        View view = LayoutInflater.from(context).inflate(R.layout.animation_loading, null);
        avi = (AVLoadingIndicatorView) view.findViewById(R.id.aviloading);
        portF = (TextView) view.findViewById(R.id.tv_prot);
        if (!StringUtils.isEmpty(msg)) portF.setText(msg);
        avi.show();
        mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mLoadingDialog.show();
        return mLoadingDialog;
    }

    public static Dialog showDialogForLoading(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.animation_loading, null);
        avi = (AVLoadingIndicatorView) view.findViewById(R.id.aviloading);
        portF = (TextView) view.findViewById(R.id.tv_prot);
        avi.show();
        mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mLoadingDialog.show();
        return mLoadingDialog;
    }

    public static void showDialogProgress(String msg) {
        if (mLoadingDialog != null && portF != null) {
            portF.setText(msg);
        }
    }

    /**
     * 关闭加载对话框
     */
    public static void cancelDialogForLoading() {
        if (mLoadingDialog != null && avi != null) {
            avi.hide();
            mLoadingDialog.cancel();
        }
    }
}
